PHP: String Interpolation

Introduction to PHP String Interpolation

String interpolation in PHP refers to the process of evaluating a string literal containing one or more placeholders. These placeholders are replaced with their corresponding values when the string is evaluated. PHP provides several ways to perform string interpolation.

Double-Quoted Strings

The simplest form of string interpolation in PHP is using double-quoted strings. PHP will automatically replace variable names with their values inside double-quoted strings.


    <?php
    $name = "John";
    $age = 30;
    echo "My name is $name and I am $age years old.";
    // Outputs: My name is John and I am 30 years old.
    ?>
                

Note: Single-quoted strings do not interpolate variables.

Using Curly Braces

For more complex expressions or to clearly delimit the variable name, you can use curly braces {}.


    <?php
    $fruit = "apple";
    echo "I ate ${fruit}s";  // Outputs: I ate apples
    
    $animals = ["dog", "cat", "bird"];
    echo "I have a {$animals[1]}";  // Outputs: I have a cat
    ?>
                

Complex Expressions

You can include more complex expressions within curly braces:


    <?php
    $quantity = 3;
    $price = 5.99;
    echo "Total: ${quantity * $price}";  // Outputs: Total: 17.97
    
    $person = ["name" => "Alice", "age" => 25];
    echo "Name: {$person['name']}";  // Outputs: Name: Alice
    ?>
                

Heredoc Syntax

Heredoc syntax provides another way to interpolate variables in multi-line strings:


    <?php
    $name = "Bob";
    $text = <<<EOT
    Hello, $name!
    This is a multi-line
    string using heredoc syntax.
    EOT;
    echo $text;
    ?>
                

Heredoc strings behave like double-quoted strings but allow for easier formatting of multi-line text.

Nowdoc Syntax

Nowdoc syntax is similar to heredoc, but it behaves like single-quoted strings (no interpolation):


    <?php
    $name = "Charlie";
    $text = <<<'EOT'
    Hello, $name!
    This string will not interpolate $name.
    EOT;
    echo $text;
    // Outputs: Hello, $name! This string will not interpolate $name.
    ?>
                

String Interpolation vs Concatenation

While string interpolation often leads to more readable code, string concatenation using the . operator is an alternative:


    <?php
    $name = "David";
    $age = 35;
    
    // String interpolation
    echo "My name is $name and I am $age years old.";
    
    // String concatenation
    echo 'My name is ' . $name . ' and I am ' . $age . ' years old.';
    
    // Both output: My name is David and I am 35 years old.
    ?>
                

Performance Considerations

While string interpolation is convenient, it can have a slight performance overhead compared to concatenation, especially for large strings or in tight loops. In most cases, the difference is negligible, but it's worth considering for performance-critical applications.

Best Practices

  • Use double-quoted strings or heredoc syntax for strings containing variables to be interpolated.
  • Use curly braces to clearly delimit variable names or for complex expressions.
  • Consider using single-quoted strings or nowdoc syntax for strings without variables to avoid unnecessary parsing.
  • Be mindful of potential security issues when interpolating user-supplied data into strings, especially in SQL queries or HTML output.

Conclusion

String interpolation in PHP provides a convenient and readable way to embed variables and expressions within strings. By understanding the different methods of string interpolation and their use cases, you can write more efficient and maintainable PHP code.